package product;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class DetailStockData extends JFrame {
	ArrayList<ProductData> plist=new ArrayList<ProductData>();//생산
	ArrayList<SaleData> slist=new ArrayList<SaleData>();//판매
	ArrayList<DetailStockData2> detail=new ArrayList<DetailStockData2>();
	DetailStockData2 dsd2;
//	DetailStockData2 dsd1;
	ProductData pd;
	SaleData sd;
	int totEa=0;
	int i;
	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnSave;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					DetailStockData frame = new DetailStockData();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public DetailStockData() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnSave());
		try {
			load();
			load2();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void load() throws Exception {// 모든 Exception를 던진다.
//		dsd2.setCommand("생산");
		dsd2=new DetailStockData2("생산");
		FileInputStream fr = new FileInputStream("C:\\ProductList.dat");
		ObjectInputStream br = new ObjectInputStream(fr);
		plist = (ArrayList)br.readObject();
		br.close();
		fr.close();
	}
	public void load2() throws Exception{// 모든 Exception를 던진다.
//		dsd2.setCommand("판매");
		dsd2=new DetailStockData2("판매");
		FileInputStream fr = new FileInputStream("C:\\SaleList.dat");
		ObjectInputStream br = new ObjectInputStream(fr);
		slist = (ArrayList)br.readObject();
		br.close();
		fr.close();
	}
	public void save() throws Exception {
		dataSave();
		FileOutputStream fw = new FileOutputStream("C:\\DetailStockList.dat");
		ObjectOutputStream bw = new ObjectOutputStream(fw);
		bw.writeObject(detail);
		bw.close();
		fw.close();

	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("load");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					
				}
			});
			btnNewButton.setBounds(60, 10, 120, 28);
		}
		return btnNewButton;
	}
	public void dataSave(){
		for(i=0;i<plist.size();i++){
			pd=plist.get(i);
			DetailStockData2 dsd2=new DetailStockData2(pd.getCode(), pd.getName(),
					pd.getVolume(),0,totEa+pd.getVolume(), pd.getNal());
			detail.add(dsd2);
			totEa=totEa+pd.getVolume();
			
		}
		for(int j=i;j<slist.size();j++){
			sd=slist.get(j);
			DetailStockData2 dsd2=new DetailStockData2(sd.getProCode(),sd.getProName(),
					0,sd.getSaleEa(),(totEa)-(sd.getSaleEa()),sd.getSaleDate());
			detail.add(dsd2);
			totEa=totEa-sd.getSaleEa();
//			System.out.println(totEa);
		}
	}
	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton("save");
			btnSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						save();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			btnSave.setBounds(248, 11, 120, 28);
		}
		return btnSave;
	}
}
